﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MongoDB.Driver;
using Teein.Proxy.Bot.Helper;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace Teein.Proxy.Bot.DAL
{
    public abstract class SQLHelper
    {
        public static readonly string ConnectionString = "mongodb://" + System.Configuration.ConfigurationManager.AppSettings["DBServer"] + "/?safe=true";
        private static MongoServer Server;
        private static string DBName = System.Configuration.ConfigurationManager.AppSettings["DBName"];
        public static MongoDatabase DB;
        private static IDictionary<string, string> CollectionNams = new Dictionary<string, string>();

        public SQLHelper()
        {
            Server = MongoDB.Driver.MongoServer.Create(ConnectionString);
            DB = Server.GetDatabase(DBName);
        }

        public static bool Insert<T>(T model)
        {
            bool result = true;

            try
            {
                DB.GetCollection(GetCollectionName<T>()).Insert(model);
            }
            catch(Exception e)
            {
                new ExceptionHelper().LogException(e);
            }

            return result;
        }

        public static bool Update<T>(IMongoQuery mq, IMongoUpdate update)
        {
            bool result = true;

            try
            {
                DB.GetCollection(GetCollectionName<T>()).Update(mq, update);
            }
            catch (Exception e)
            {
                new ExceptionHelper().LogException(e);
            }

            return result;
        }


        public static bool Delete<T>(IMongoQuery mq)
        {
            bool result = true;

            try
            {
                DB.GetCollection(GetCollectionName<T>()).Remove(mq);
            }
            catch (Exception e)
            {
                new ExceptionHelper().LogException(e);
            }

            return result;
        }

        public static IList<T> Find<T>(IMongoQuery mq)
        {
            IList<T> result = new List<T>();
            try
            {
                if (mq == null)
                {
                    MongoCollection mc=  DB.GetCollection(GetCollectionName<T>());
                    MongoCursor<T> temp = mc.FindAllAs<T>();

                    foreach (var item in temp)
                    {
                        result.Add((T)item);
                    }
                }
                else
                {
                    result = DB.GetCollection(GetCollectionName<T>()).FindAs<T>(mq).ToList<T>();
                }
            }
            catch (Exception e)
            {
                new ExceptionHelper().LogException(e);
            }

            return result;
        }

        public static T FindOne<T>(IMongoQuery mq)
        {
            T result = default(T);
            try
            {
                result = DB.GetCollection(GetCollectionName<T>()).FindOneAs<T>(mq);
            }
            catch (Exception e)
            {
                new ExceptionHelper().LogException(e);
            }

            return result;
        }

        private static string GetCollectionName<T>()
        {
            string collectionName;
            if (!CollectionNams.Keys.Contains(typeof(T).FullName))
            {
                var TFName = typeof(T).FullName.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                collectionName = TFName[TFName.Length - 1];
                CollectionNams.Add(typeof(T).FullName, collectionName);
            }
            else
            {
                collectionName = CollectionNams[typeof(T).FullName];
            }

            return collectionName;
        }
    } 
}
